home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / src / Tools / vtcl / doc / Async.3 < prev    next >
Encoding:
Text File  |  1995-07-10  |  7.4 KB  |  180 lines

  1. '\"
  2. '\" Copyright (c) 1989-1993 The Regents of the University of California.
  3. '\" All rights reserved.
  4. '\"
  5. '\" Permission is hereby granted, without written agreement and without
  6. '\" license or royalty fees, to use, copy, modify, and distribute this
  7. '\" documentation for any purpose, provided that the above copyright
  8. '\" notice and the following two paragraphs appear in all copies.
  9. '\"
  10. '\" IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
  11. '\" FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
  12. '\" ARISING OUT OF THE USE OF THIS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
  13. '\" CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  14. '\"
  15. '\" THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
  16. '\" INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
  17. '\" AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
  18. '\" ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO
  19. '\" PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
  20. '\" 
  21. '\" $Header: /user6/ouster/tcl/man/RCS/Async.3,v 1.5 93/09/17 15:21:50 ouster Exp $ SPRITE (Berkeley)
  22. '\" 
  23. '\"----------------------------------------------------------------------------
  24. '\"    @(#) Async.3 26.1 93/10/22 SCOINC
  25. '\"
  26. '\"     Copyright (C) The Santa Cruz Operation, 1992-1993.
  27. '\"     This Module contains Proprietary Information of
  28. '\"    The Santa Cruz Operation, and should be treated as Confidential.
  29. '\"----------------------------------------------------------------------------
  30. .so ../man.macros
  31. .HS Tcl_AsyncCreate tclc 7.0
  32. .BS
  33. .SH NAME
  34. Tcl_AsyncCreate, Tcl_AsyncMark, Tcl_AsyncInvoke, Tcl_AsyncDelete \- handle asynchronous events
  35. .SH SYNOPSIS
  36. .nf
  37. \fB#include <tcl.h>\fR
  38. .sp
  39. extern int \fBtcl_AsyncReady\fR;
  40. .sp
  41. Tcl_AsyncHandler
  42. \fBTcl_AsyncCreate\fR(\fIproc, clientData\fR)
  43. .sp
  44. \fBTcl_AsyncMark\fR(\fIasync\fR)
  45. .sp
  46. int
  47. \fBTcl_AsyncInvoke\fR(\fIinterp, code\fR)
  48. .sp
  49. \fBTcl_AsyncDelete\fR(\fIasync\fR)
  50. .SH ARGUMENTS
  51. .AS Tcl_AsyncHandler clientData
  52. .AP Tcl_AsyncProc *proc in
  53. Procedure to invoke to handle an asynchronous event.
  54. .AP ClientData clientData in
  55. One-word value to pass to \fIproc\fR.
  56. .AP Tcl_AsyncHandler async in
  57. Token for asynchronous event handler.
  58. .AP Tcl_Interp *interp in
  59. Tcl interpreter in which command was being evaluated when handler was
  60. invoked, or NULL if handler was invoked when there was no interpreter
  61. active.
  62. .AP int code in
  63. Completion code from command that just completed in \fIinterp\fR,
  64. or 0 if \fIinterp\fR is NULL.
  65. .BE
  66.  
  67. .SH DESCRIPTION
  68. .PP
  69. These procedures provide a safe mechanism for dealing with
  70. asynchronous events such as signals.
  71. If an event such as a signal occurs while a Tcl script is being
  72. evaluated then it isn't safe to take any substantive action to
  73. process the event.
  74. For example, it isn't safe to evaluate a Tcl script since the
  75. intepreter may already be in the middle of evaluating a script;
  76. it may not even be safe to allocate memory, since a memory
  77. allocation could have been in progress when the event occurred.
  78. The only safe approach is to set a flag indicating that the event
  79. occurred, then handle the event later when the world has returned
  80. to a clean state, such as after the current Tcl command completes.
  81. .PP
  82. \fBTcl_AsyncCreate\fR creates an asynchronous handler and returns
  83. a token for it.
  84. The asynchronous handler must be created before
  85. any occurrences of the asynchronous event that it is intended
  86. to handle (it is not safe to create a handler at the time of
  87. an event).
  88. When an asynchronous event occurs the code that detects the event
  89. (such as a signal handler) should call \fBTcl_AsyncMark\fR with the
  90. token for the handler.
  91. \fBTcl_AsyncMark\fR will mark the handler as ready to execute, but it
  92. will not invoke the handler immediately.
  93. Tcl will call the \fIproc\fR associated with the handler later, when
  94. the world is in a safe state, and \fIproc\fR can then carry out
  95. the actions associated with the asynchronous event.
  96. \fIProc\fR should have arguments and result that match the
  97. type \fBTcl_AsyncProc\fR:
  98. .nf
  99. .RS
  100. typedef int Tcl_AsyncProc(
  101. .RS
  102. ClientData \fIclientData\fR,
  103. Tcl_Interp *\fIinterp\fR,
  104. int \fIcode\fR);
  105. .RE
  106. .RE
  107. .fi
  108. The \fIclientData\fR will be the same as the \fIclientData\fR
  109. argument passed to \fBTcl_AsyncCreate\fR when the handler was
  110. created.
  111. If \fIproc\fR is invoked just after a command has completed
  112. execution in an interpreter, then \fIinterp\fR will identify
  113. the interpreter in which the command was evaluated and
  114. \fIcode\fR will be the completion code returned by that
  115. command.
  116. The command's result will be present in \fIinterp->result\fR.
  117. When \fIproc\fR returns, whatever it leaves in \fIinterp->result\fR
  118. will be returned as the result of the command and the integer
  119. value returned by \fIproc\fR will be used as the new completion
  120. code for the command.
  121. .PP
  122. It is also possible for \fIproc\fR to be invoked when no interpreter
  123. is active.
  124. This can happen, for example, if an asynchronous event occurs while
  125. the application is waiting for interactive input or an X event.
  126. In this case \fIinterp\fR will be NULL and \fIcode\fR will be
  127. 0, and the return value from \fIproc\fR will be ignored.
  128. .PP
  129. The procedure \fBTcl_AsyncInvoke\fR is called to invoke all of the
  130. handlers that are ready.
  131. The global variable \fBtcl_AsyncReady\fR will be non-zero whenever any
  132. asynchronous handlers are ready;  it can be checked to avoid calls
  133. to \fBTcl_AsyncInvoke\fR when there are no ready handlers.
  134. Tcl checks \fBtcl_AsyncReady\fR after each command is evaluated
  135. and calls \fBTcl_AsyncInvoke\fR if needed.
  136. Applications may also call \fBTcl_AsyncInvoke\fR at interesting
  137. times for that application.
  138. For example, Tk's event handler checks \fBtcl_AsyncReady\fR
  139. after each event and calls \fBTcl_AsyncInvoke\fR if needed.
  140. The \fIinterp\fR and \fIcode\fR arguments to \fBTcl_AsyncInvoke\fR
  141. have the same meaning as for \fIproc\fR:  they identify the active
  142. intepreter, if any, and the completion code from the command
  143. that just completed.
  144. .PP
  145. \fBTcl_AsyncDelete\fR removes an asynchronous handler so that
  146. its \fIproc\fR will never be invoked again.
  147. A handler can be deleted even when ready, and it will still
  148. not be invoked.
  149. .PP
  150. If multiple handlers become active at the same time, the
  151. handlers are invoked in the order they were created (oldest
  152. handler first).
  153. The \fIcode\fR and \fIinterp->result\fR for later handlers
  154. reflect the values returned by earlier handlers, so that
  155. the most recently created handler has last say about
  156. the interpreter's result and completion code.
  157. If new handlers become ready while handlers are executing,
  158. \fBTcl_AsyncInvoke\fR will invoke them all;  at each point it
  159. invokes the highest-priority (oldest) ready handler, repeating
  160. this over and over until there are no longer any ready handlers.
  161.  
  162. .SH WARNING
  163. .PP
  164. It is almost always a bad idea for an asynchronous event
  165. handler to modify \fIinterp->result\fR or return a code different
  166. from its \fIcode\fR argument.
  167. This sort of behavior can disrupt the execution of scripts in
  168. subtle ways and result in bugs that are extremely difficult
  169. to track down.
  170. If an asynchronous event handler needs to evaluate Tcl scripts
  171. then it should first save \fIinterp->result\fR plus the values
  172. of the variables \fBerrorInfo\fR and \fBerrorCode\fR (this can
  173. be done, for example, by storing them in dynamic strings).
  174. When the asynchronous handler is finished it should restore
  175. \fIinterp->result\fR, \fBerrorInfo\fR, and \fBerrorCode\fR,
  176. and return the \fIcode\fR argument.
  177.  
  178. .SH KEYWORDS
  179. asynchronous event, handler, signal
  180.